<?php

namespace app\utils\business;

use app\admin\controller\Index;
use think\Model;
use think\Request;

/**
 * @property Model $model
 * @mixin \app\store\controller\Controller
 */
trait BackendCURD
{
    /**
     * 查看 列表
     */
    public function index()
    {
        $list = $this->model->getList($this->request->get());
        return $this->fetch('index', compact('list'));
    }
    
    protected function addBefore()
    {
    
    }
    
    /**
     * 添加
     */
    public function add()
    {
        $this->addBefore();
        $model = $this->model;
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        // 新增记录
        if ($model->add($this->postData('model'))) {
            return $this->renderSuccess('添加成功', url('index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
    
    protected function editBefore()
    {
    
    }
    
    /**
     * 编辑
     */
    public function edit($id)
    {
        $this->editBefore();
        // 校区详情
        $model = $this->model->detail($id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        // 新增记录
        if ($model->edit($this->postData('model'))) {
            return $this->renderSuccess('更新成功', url('index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
    
    /**
     * 删除
     * @param int $id
     * @return array|bool
     */
    public function del(int $id)
    {
        // 校区详情
        $model = $this->model->detail($id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
    
    public function detail($id)
    {
        // 详情
        /** @var Model $model */
        $model = $this->model->detail($id);
        if (empty($model)) {
            return $this->renderSuccess('该数据不存在！');
        }
        return $this->fetch('detail', compact('model'));
    }
    
    /**
     * 删除校区
     * @param $id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($id)
    {
        // 详情
        /** @var Model $model */
        $model = $this->model->detail($id);
        if (empty($model)) {
            return $this->renderSuccess('该数据不存在！');
        }
        
        if ($model->fieldExists('is_delete')) {
            if ($model['is_delete']) {
                return $this->renderSuccess('该数据已经被删除！');
            }
            if (!$model->setDelete()) {
                return $this->renderError($model->getError() ?: '删除失败');
            }
            // return $this->renderError($model->getError() ?: '删除失败');
        } else if (!$model->delete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
    
    
    /**
     * 加载为select(远程下拉选择框)数据，默认还是走$this->index()方法
     * 必要时请在对应控制器类中重写
     */
    public function select()
    {
    
    }
}